package com.tang.deliver.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tang.common.api.CompanyJobFeignClient;
import com.tang.common.entity.companyJob.Job;
import com.tang.common.entity.deliver.Deliver;
import com.tang.common.entity.deliver.JobCollect;
import com.tang.common.entity.deliver.Resume;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.deliver.service.JobCollectService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-21
 */
@RestController
@RequestMapping("/jobCollect")
public class JobCollectController {
    @Autowired
    private JobCollectService jobCollectService;

    @Autowired
    private CompanyJobFeignClient companyJobFeignClient;

    @RequestMapping("/collectJob")
    public Result collectJob(Integer userId, Integer jobId) {
        if (userId != null && jobId != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("job_id", jobId);
            JobCollect jobCollect = jobCollectService.getOne(queryWrapper);
            if (jobCollect != null) {
                return Result.error(RecruitExceptionEnum.HAS_COLLECT);
            }
            jobCollect = new JobCollect();
            jobCollect.setJobId(jobId);
            jobCollect.setUserId(userId);
            boolean save = jobCollectService.save(jobCollect);
            if (save) {
                return Result.success("收藏成功");
            }
        }
        return Result.error(RecruitExceptionEnum.COLLECT_FAILED);
    }

    @RequestMapping("/deleteCollect")
    public Result deleteCollect(Integer jobCollectId) {
        if (jobCollectId != null) {
            boolean b = jobCollectService.removeById(jobCollectId);
            if (b) {
                return Result.success("删除成功");
            }
        }
        return Result.error(RecruitExceptionEnum.DELETE_FAILED);
    }

    @RequestMapping("/jobCollectList")
    public Result jobCollectList(Integer userId) {
        QueryWrapper<JobCollect> query = new QueryWrapper();
        query.eq("delete_flag", 1);
        query.eq("user_id", userId);
        List<JobCollect> list = jobCollectService.list(query);
        if (list != null) {
            for (JobCollect jobCollect : list) {
                Integer jobId = jobCollect.getJobId();
                Job feignJob = companyJobFeignClient.getFeignJob(jobId);
                System.out.println(feignJob);
                if (feignJob != null) {
                    jobCollect.setJob(feignJob);
                }
            }
        }
        return Result.success(list);
    }
}
